//
//  VDDetailListController.m
//  Runone
//
//  Created by runone01 on 16/8/11.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "VDDetailListController.h"
#import "VDDeatilListCell.h"
#import "VideoSetting.h"
#import "DeviceBaseInfo.h"
#import "TestViewController.h"

#import "DeviceBaseInfo.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "DMVDCurrentDataInfo.h"
#import "UIView+Message.h"

#import "VDMessageViewController.h"

@interface VDDetailListController ()<UITableViewDelegate, UITableViewDataSource,UIPopoverPresentationControllerDelegate>
@property (strong, nonatomic)DeviceBaseInfo *Camera;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)DMVDCurrentDataInfo *VD;
@property(nonatomic,strong)NSString *direct;
@property(nonatomic,strong)VDMessageViewController *vdMessage;
@end
static NSString *const cellId =@"cell";
@implementation VDDetailListController{
    UITableView *vdTalbieView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"VD详情";
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text=@"VD详情";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    
    vdTalbieView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    
    vdTalbieView.dataSource = self;
    vdTalbieView.delegate = self;
    vdTalbieView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [vdTalbieView registerNib:[UINib nibWithNibName:NSStringFromClass([VDDeatilListCell class]) bundle:nil] forCellReuseIdentifier:cellId];
    vdTalbieView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:vdTalbieView];

}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

-(void)request
{
    
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetDeviceDataURL;
    getCurrentEventModel.DataValue= [self DataValueStr:_Camera.DeviceUID];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"GetDeviceSettingInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        NSLog(@"%@",requestOutginseng.DataValue);
        _vdMessage=[[VDMessageViewController alloc] init];
        NSDictionary *dic=[requestOutginseng.DataValue mj_JSONObject];
        NSArray *info = [[dic objectForKey:@"DeviceData"] mj_JSONObject];
        _vdMessage.dataSource=info;
        _contentView=MessageViewFrame;
      //  _contentView.backgroundColor=[UIColor whiteColor];
        if (info.count>0) {
//            for (NSDictionary *vdDic in info) {
//                _VD=[DMVDCurrentDataInfo mj_objectWithKeyValues:vdDic];
//                if ([_VD.DIRECTION_NO isEqualToString:@"1"]) {
//                    _direct=@"乐昌往广州";
//                }
//                if ([_VD.DIRECTION_NO isEqualToString:@"2"]) {
//                    _direct=@"广州往乐昌";
//                }
//                if ([_VD.DIRECTION_NO isEqualToString:@"3"]) {
//                    _direct=@"双向";
//                }
//                [_contentView DeviceMessage:[NSString stringWithFormat:@"车道: %@\n量值: %@\n速度值: %@\n占有率: %@\n时间:%@",_VD.LANE_NO,_VD.VOLUME,_VD.SPEED,_VD.OCCUPANCY,_VD.TIME_ID]];
//            }
            [self picker];
        }else
        {
            [_contentView prompt:@" VD暂无内容! "];
            [self.view addSubview:_contentView];
        }
        
        
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(void)picker {
    _vdMessage.modalPresentationStyle=UIModalPresentationPopover;
    _vdMessage.popoverPresentationController.sourceView=self.view;
    _vdMessage.popoverPresentationController.sourceRect=CGRectMake(self.view.center.x, self.view.center.y, 0, 0);
    _vdMessage.popoverPresentationController.permittedArrowDirections=NO;
    _vdMessage.popoverPresentationController.delegate=self;
    [self presentViewController:_vdMessage animated:YES completion:nil];
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    _contentView.hidden=YES;
    [_contentView removeFromSuperview];
}
-(NSString *)DataValueStr:(NSString *)str {
    NSString *beginstr=[NSString stringWithFormat:@"[{\"key\":\"DeviceUID\",\"value\":\"%@\"}]",str];
    
    return beginstr;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VDDeatilListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[VDDeatilListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    DeviceBaseInfo *vdSetting = self.contentArray[indexPath.row];
//    VideoSetting *setModel = vdSetting.setModel;
    cell.pileNOlabel.text = [NSString stringWithFormat:@"桩号：%@+%ld",vdSetting.PileNo,(long)vdSetting.PileDistance];
    NSString *derectionStr = [NSString stringWithFormat:@"%ld",(long)vdSetting.Direction];
    NSString *str = nil;
    if ([derectionStr isEqualToString:@"1"]) {
        str = @"往乐昌方向";
    }else {
        str = @"往广州方向";
    }
    cell.derectionLabel.text = [NSString stringWithFormat:@"方向:%@",str];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _Camera = self.contentArray[indexPath.row];
    [self request];
//    TestViewController *testVC=[[TestViewController alloc] init];
//    [self.navigationController pushViewController:testVC animated:YES];
}

@end
